/*
 * ApplicationStorage.cs - Wordtrainer
 * 
 * Copyright (C) 1998-2007 Markus Kilås
 *               
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 * Authors:
 *   Markus Kilås <markus@markuspage.com>
 * 
 */

using System;
using System.Collections;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace WtSharp
{
	
	[Serializable]
	public class ApplicationStorage : Hashtable
	{
		private string settingsFileName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ".dat";
		
		public ApplicationStorage()
		{
			LoadData();
		}
		
		protected ApplicationStorage (SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
		
		public void LoadData()
		{
			IsolatedStorageFile file = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, "Wordtrainer", "Wordtrainer");
			if(file.GetFileNames(settingsFileName).Length == 0)
				return;
			
			Stream stream = new IsolatedStorageFileStream(settingsFileName, FileMode.OpenOrCreate, file);
			if(stream == null)
				return;
			
			try
			{
				IFormatter formatter = new BinaryFormatter();
				Hashtable appData = (Hashtable)formatter.Deserialize(stream);
				IDictionaryEnumerator en = appData.GetEnumerator();
				while(en.MoveNext())
				{
					this[en.Key] = en.Value;
				}
			}
			catch(EndOfStreamException ex)
			{
				Console.WriteLine("Warning: The settings database was corrupted");
				Console.WriteLine(ex);
				Console.WriteLine(ex.StackTrace);
			}
			catch(SerializationException ex)
			{
				Console.WriteLine("Warning: The settings database format changed in this version of Wordtrainer");
				Console.WriteLine(ex);
				Console.WriteLine(ex.StackTrace);
			}
			finally
			{
				stream.Close();
			}
		}
		
		public void Save()
		{
			IsolatedStorageFile store = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, "Wordtrainer", "Wordtrainer");
			Stream stream = new IsolatedStorageFileStream(settingsFileName, FileMode.Create, store);
			if(stream == null)
				return;
			try
			{
				IFormatter formatter = new BinaryFormatter();
				formatter.Serialize(stream, (Hashtable)this);
			}
			finally
			{
				stream.Close();
			}
		}
	}
}
